// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import ElementUI from 'element-ui'
import ElementVerify from 'element-verify'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/theme-chalk/icon.css'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import router from './router'
import http from './api/http'
import permission from './api/permission'
import store from './store/index'
import base from './utilities/common'
import '@/utils/adapter.js'
import Print from 'vue-print-nb'
import { VueJsonp } from 'vue-jsonp'
import '@/scss/index.scss'
import jump from '@/utils/jump'
import globalMethods from '@/utils/globalMethods'
import jrQrcode from "jr-qrcode";
import draggable from 'vuedraggable'
import './extension/system/flow/Sys_WorkFlow/index.css'
import _ from 'lodash'
import { jsonToFormData } from 'kayran'
import vueSeamlessScroll from 'vue-seamless-scroll'
Vue.use(vueSeamlessScroll)
Vue.prototype._ = _
import {
  getWrapStyle,
  getComponentStyle,
  getMultiBackground
} from '@/utils/style'
import elformdesign from 'form-design-reg-exp'
import ConfigCtn from '@/components/global/ConfigCtn.vue';
import ConfigItem from '@/components/global/ConfigItem.vue';
import ConfigWrap from '@/components/global/ConfigWrap.vue';
// import GitControl from '@/components/global/GitControl.vue';
import phoneCtn from '@/components/global/phoneCtn/index.vue';
import SchemaJump from '@/components/global/JumpSelect/index.vue';
import JumpDialog from '@/components/global/JumpSelect/JumpDialog.vue';
Vue.component('phoneCtn', phoneCtn);
Vue.component('ConfigCtn', ConfigCtn);
Vue.component('ConfigItem', ConfigItem);
Vue.component('ConfigWrap', ConfigWrap);
// Vue.component('GitControl', GitControl);
Vue.component('SchemaJump', SchemaJump);
Vue.component('JumpDialog', JumpDialog);
Vue.use(globalMethods)
Vue.use(VueJsonp)
Vue.use(iView)
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(ElementVerify)
Vue.use(Print)

import 'imgpond/dist/style.css'
import Imgpond from 'imgpond'

let fields = {}
let initializing = {}

const importAll = async () => {
  const files = import.meta.globEager('./widgets/*/component.json')
  const fileKeys = Object.keys(files)

  await Promise.all(fileKeys.map(async (key) => {
    const [, name] = key.split('/')
    const file = files[key]
    let config = { component: name, ...file }

    fields[name] = config.fields
    initializing[name] = initDefaulValue(config)
  }))

  Vue.prototype.$fields = fields
  Vue.prototype.$initializing = initializing
}

importAll()


Vue.use(Imgpond, {
  upload: (file, context) => new Promise((resolve, reject) => {
    axios.post(http.ipAddress + 'api/Small_user_distributors_system/Upload_OSS', jsonToFormData({
      fileInput: file,
      type:1,
      storage:"OSS_X",//头像 OSS_H  小程序 OSS_X   隐私 OSS_Y  默认（OSS） OSS
      ...context.$attrs.requestParam,
    }), {
      headers: {
        "Content-Type": "multipart/form-data"
      },
      baseURL: '', // 针对prod环境中baseAPI为相对路径的情况
      timeout: 20000,
    }).then(res => {
      resolve( res.data.data[0].file_name)
    }).catch(e => {
      reject(e)
    })
  }),
})
Vue.component('draggable', draggable)
Vue.use(elformdesign);

Vue.prototype.$jump = jump
Vue.prototype.http = http;

Vue.prototype.permission = permission;

Vue.prototype.base = base;
Vue.prototype.$getWrapStyle = getWrapStyle
Vue.prototype.$getComponentStyle = getComponentStyle
Vue.prototype.$getMultiBackground = getMultiBackground

/* 2020.07.31增加手动打开tabs*/
Vue.prototype.$tabs = {};

Vue.prototype.$addStorageEvent = function (type, key, data) {
  if (type === 1) {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        sessionStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  } else {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        sessionStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  }
}
// new Vue({
//   el: '#app',
//   store,
//   router,
//   components: { App },
//   template: '<App/>'
// })
// 使用 import.meta.glob 导入所有全局组件
// import { createApp } from 'vue';
// const components = import.meta.glob(['@/components/global/*.vue', '@/custom-schema-template/*.vue','@/custom-schema-template-set/config-template/*.vue','@/custom-schema-template/*.vue']);

// const app = createApp(App);

// // 遍历导入的组件并注册
// for (const [path, component] of Object.entries(components)) {
//   // 从路径中提取组件名称
//   const componentName = path.match(/\.\/components\/global\/(.*)\.vue$/)[1];
//   // 注册全局组件
//   app.component(componentName, (await component()).default);
// }


// 调用函数
// registerComponentsFromFolders(['@/components/global', '@/custom-schema-template','@/custom-schema-template-set/config-template','@/custom-schema-template']);


let vue = new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')


Vue.prototype.http.init(vue);
Vue.prototype.permission.init(vue);
